<template>
  <div class="home">
    <h1>停车场管理系统</h1>
    <img src="../assets/imgs/shark.png">
    <h2>WeilaiParking</h2>
    <el-card style="width: 600px;height: 200px;margin: 0 auto;
    background-color: rgba(255,255,255,0.3)">

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item style="position: relative;right: 25px">
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="reg()">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<style>
body {
  text-align: center;
  background-image: url("../assets/imgs/background.jpg");
  margin: 0; /*去掉自带外边距*/
  background-size: cover; /*cove  把背景图片设置为封面  设置之后  背景图片 不会随着浏览器缩放而缩放 会自动填充 */
  background-repeat: no-repeat; /*背景图片不重复*/
}

h1 {
  font-size: 72px;
  color: #0096dc;
  margin-bottom: 0;
}

h2 {
  font-size: 32px;
  color: #0096dc;
  margin-bottom: 0;
}
</style>
<script>
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password: '',
        user: {}
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],

      }

    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        let url = 'http://localhost:8080/user/login'
        console.log(this.ruleForm)
        this.axios.post(url, this.ruleForm).then((response) => {
          let responseBody = response.data;
          if (responseBody.code == 200) {
            this.ruleForm.user = responseBody.data;
            let cardId = this.ruleForm.user.cardId;
            localStorage.setItem('cardId', cardId)
            localStorage.setItem('realName',this.ruleForm.user.realName)
            localStorage.setItem('phone',this.ruleForm.user.phone)
            localStorage.setItem('nickname',this.ruleForm.user.nickname)
            console.log(this.ruleForm.user.nickname)
            console.log(this.ruleForm.user.cardId)
            this.$message({
              showClose: true,
              message: '登录成功！',
              type: 'success'
            });
            this.$router.push('/UserIndex');
          } else {
            this.$message({
              showClose: true,
              message: responseBody.message,
              type: 'error'
            });
          }
        })
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    reg() {
      this.$router.push('/reg');
    }
  }
}

</script>
